package com.weifly.egp.gui;

import com.weifly.egp.service.monitor.StockInfo;
import com.weifly.egp.service.monitor.StockMonitorService;
import com.weifly.egp.service.trade.StockOrderInfo;

import javax.swing.table.AbstractTableModel;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by weijianxin on 2018/8/26.
 */
public class StockTableModel extends AbstractTableModel{

    public static final int COLUMN_stockName = 0;
    public static final int COLUMN_stockCode = 1;
    public static final int COLUMN_newestPrice = 2;

    public static final int COLUMN_buyStatus = 3;
    public static final int COLUMN_sellStatus = 4;

    public static final int COLUMN_basePrice = 5;
    public static final int COLUMN_gridPrice = 6;
    public static final int COLUMN_tradeUnit = 7;

    private SimpleDateFormat timeSdf = new SimpleDateFormat("HH:mm:ss");

    private StockMonitorService stockMonitorService;

    public StockTableModel(StockMonitorService stockMonitorService){
        this.stockMonitorService = stockMonitorService;
    }

    private String[] columnNames = new String[]{"名称","代码","最新价","买入委托","卖出委托","基准价格","网格价差","每次交易量"};

    private Class[] columnClasses = new Class[]{String.class, String.class, Double.class, String.class, String.class, Double.class, Double.class, Integer.class};

    public int getRowCount() {
        return this.stockMonitorService.getStockList().size();
    }

    public int getColumnCount() {
        return this.columnNames.length;
    }

    public String getColumnName(int column) {
        return this.columnNames[column];
    }

    public Class<?> getColumnClass(int column){
        return this.columnClasses[column];
    }

    public Object getValueAt(int rowIndex, int columnIndex) {
        StockInfo stockInfo = this.stockMonitorService.getStockList().get(rowIndex);
        if(stockInfo==null){
            return null;
        }
        Object value = null;
        if(columnIndex==COLUMN_stockName){
            value = stockInfo.getStockName();
        }else if(columnIndex==COLUMN_stockCode){
            value = stockInfo.getStockCode();
        }else if(columnIndex==COLUMN_newestPrice){
            // 最新价
            value = stockInfo.getNewestPrice();
        }else if(columnIndex==COLUMN_buyStatus){
            // 买入委托
            if(stockInfo.getBuyOrderInfo()!=null){
                StockOrderInfo orderInfo = stockInfo.getBuyOrderInfo();
                StringBuilder sb = new StringBuilder();
                sb.append("单号:").append(orderInfo.getOrderNumber());
                sb.append(",价格:").append(orderInfo.getEntrustPrice());
                sb.append(",时间:").append(this.timeSdf.format(new Date(orderInfo.getOrderTime())));
                value = sb.toString();
            }
        }else if(columnIndex==COLUMN_sellStatus){
            // 卖出委托
            if(stockInfo.getSellOrderInfo()!=null){
                StockOrderInfo orderInfo = stockInfo.getSellOrderInfo();
                StringBuilder sb = new StringBuilder();
                sb.append("单号:").append(orderInfo.getOrderNumber());
                sb.append(",价格:").append(orderInfo.getEntrustPrice());
                sb.append(",时间:").append(this.timeSdf.format(new Date(orderInfo.getOrderTime())));
                value = sb.toString();
            }
        }else if(columnIndex==COLUMN_basePrice){
            // 基准价格
            value = stockInfo.getBasePrice();
        }else if(columnIndex==COLUMN_gridPrice){
            // 网格价差
            value = stockInfo.getGridPrice();
        }else if(columnIndex==COLUMN_tradeUnit){
            // 每次交易量
            value = stockInfo.getTradeUnit();
        }
        return value;
    }
}
